﻿using UnityEngine;
using System.IO;
using System.Text;
using System;
using System.Collections.Generic;


namespace OpenPose.MyInterface
{
    public class OpenPoseOutput : MonoBehaviour
    {
        public enum Input_Stream
        {
            Openpose_Plugin,
            File,
            None //不开启openpose
                
        }
        public Input_Stream input_type = Input_Stream.Openpose_Plugin;

        // 使用File作为输入流时 需要指定json的读取文件夹
        public string file_input_root = "E:/ZHUODONG LI/Work/CHI/CHIDesign03/outer_resource/openpose_output/test";

        public uint MAX_FLAME_CACHE = 10;

        // Output
        private OpenPoseUserScript openPoseUser;
        private PoseKeyPoints current_kp;
        private PoseTimeSeries history;
        //public DateTime start_time;
        

        // Use this for initialization
        void Start()
        {
            openPoseUser = GetComponent<OpenPoseUserScript>();
            //start_time = DateTime.Now;
            history = new PoseTimeSeries((int)(MAX_FLAME_CACHE));
            
            //清空输出文件夹
            if (input_type == Input_Stream.File)
            {
                //TODO
                throw new NotImplementedException();
            }
        }

        // Update is called once per frame
        void Update()
        {
            if (input_type == Input_Stream.Openpose_Plugin)
            {
                if (openPoseUser != null)
                {
                    MultiArray<float> poseKeypoints = openPoseUser.getDatum().poseKeypoints;
                    // OpenPoseUserScript的getDatum()可能返回空值
                    if (poseKeypoints != null)
                    {
                        current_kp = new PoseKeyPoints(poseKeypoints, DateTime.Now);
                        history.add_poseKeyPoints(current_kp);
                    }
                }
                else
                {
                    openPoseUser = GetComponent<OpenPoseUserScript>();
                }
            }
            if (Input_Stream.File == input_type)
            {
                //实时读取文件作为openpose输出
                //TODO
                throw new NotImplementedException();
            }


        }

        /// <summary>
        /// 获取用户最近的一帧骨骼 
        /// </summary>
        /// <returns></returns>
        public PoseKeyPoints get_current_kp()
        {
            return current_kp;
        }
        
        public List<PoseKeyPoints> GetPoseSeg(DateTime start, DateTime end)
        {
            return history.poseInSeg(start,end);
        }
    }
}

